const fs = require("fs");
const defaultGlobal = require("../../default/global");

function getGlobal(template) {
    // const global = JSON.parse(fs.readFileSync(`./src/theme/${template}/lib/config/global.json`, "utf-8"));
    // const settingsGlobalData = {};
    // global.forEach((element) => {
    //     element.settings.forEach((setting) => {
    //         settingsGlobalData[setting.id] = setting.default;
    //     });
    // });
    return getDiffGlobal(template); //{ settingsGlobalData, global };
}

function mergeDiffConfig(setting, option) {
    Object.keys(option).forEach((key) => {
        //如果是数组
        if (Array.isArray(setting[key])) {
            if (!setting[key].length) {
                setting[key] = Object.values(option[key]);
            } else {
                setting[key].forEach((item, index) => {
                    if (option[key][index]) {
                        mergeDiffConfig(item, option[key][index]);
                    }
                });
            }
        } else if (typeof setting[key] === "object" && setting[key] !== null) {
            mergeDiffConfig(setting[key], option[key]);
        } else {
            setting[key] = option[key];
        }
    });
    return setting;
}
// 测试代码

// const setting = {
//     type: "card_font",
//     id: "general_font",
//     label: {
//         zh_CN: "正文",
//         en_US: "The text"
//     },
//     default: {
//         handle: "frank_ruhl_libre_n4",
//         family: "Frank Ruhl Libre",
//         style: "normal",
//         weigth: 400,
//         font_face:
//             "@font-face {  font-family: 'Frank Ruhl Libre';  font-style: normal;  font-weight: 400;  font-display: swap;  src: url(https://fonts.gstatic.com/s/frankruhllibre/v6/j8_w6_fAw7jrcalD7oKYNX0QfAnPW7Dl4Q.woff2) format('woff2');}"
//     }
// }
// mergeDiffConfig(
//     setting,
//     {
//         "default": {
//             "handle": "Alatsi",
//             "family": "Alatsi",
//             "font_face": "@font-face {font-family: 'Alatsi';font-style: normal;font-weight: 400;font-display: swap;src: url(https://fonts.gstatic.com/s/alatsi/v11/TK3iWkUJAxQ2nLN2GT8Gl-1PKw.woff2) format('woff2');unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;}"
//         }
//     }
// );
// console.log(setting);
function getDiffGlobal(template) {
    const newDefaultGlobal = JSON.parse(JSON.stringify(defaultGlobal));
    const settingsGlobalData = {};

    try {
        const stat = fs.statSync(`./src/theme/${template}/lib/config/diff_global.json`);
        if (stat) {
            const diff = JSON.parse(
                fs.readFileSync(`./src/theme/${template}/lib/config/diff_global.json`, "utf-8")
            );
            const diffConfigKeys = Object.keys(diff.diffConfig);
            if (diffConfigKeys.length) {
                newDefaultGlobal.forEach((element) => {
                    element.settings.forEach((setting) => {
                        if (diffConfigKeys.includes(setting.id)) {
                            mergeDiffConfig(setting, diff.diffConfig[setting.id]);
                        }
                    });
                });
            }

            if (diff.addConfig.length) {
                diff.addConfig.forEach((item) => {
                    const keys = item.key.split("-");
                    newDefaultGlobal[keys[0]].settings.splice(keys[1], 0, item);
                });
            }
        }
    } catch (error) {
        console.log("不存在diff_global.json");
    }

    newDefaultGlobal.forEach((element) => {
        element.settings.forEach((setting) => {
            settingsGlobalData[setting.id] = setting.default;
        });
    });

    return { settingsGlobalData, global: newDefaultGlobal };
}
module.exports = { getGlobal };
